home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / FILECMDS.C < prev    next >
Text File  |  1992-12-02  |  15KB  |  571 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  */
  55.  
  56. #include    "SystemPub.h"
  57. #include    "Proc.h"
  58. #include    "ShellPub.h"
  59. #include    "path.h"
  60.  
  61. #define        BUFSIZE        256
  62.  
  63. /*******************************************************************
  64.  *
  65.  *    Function CP
  66.  *
  67.  *    PathName Callback function
  68.  *
  69.  *    usage CP file file
  70.  *          CP files directory
  71.  *
  72.  *******************************************************************/
  73.  
  74. #define        cpVRefNum        (**MyShell).Proc[ProcID].int0
  75. #define        cpDirID            (**MyShell).Proc[ProcID].long0
  76. #define        cpAbort            (**MyShell).Proc[ProcID].bflags.f0
  77.  
  78. Boolean        CPSetDestDir( WHandle ShellWh, int16 ProcID, char *argument )
  79. {
  80. int16        err;
  81. pathType    pt;
  82. char        dirPath[ 256 ];
  83. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  84.  
  85.     strcpy( dirPath, argument );
  86.     pt = SetCurrPath( dirPath );
  87.     if( pt == pathIsDir )
  88.         {
  89.         err = HGetVol( NULL, &cpVRefNum, &cpDirID );
  90.         if( err )
  91.             FileError( err );
  92.         }
  93.     else
  94.         err = 1;
  95.  
  96.     ResetShellPWD( ShellWh );
  97.     return( err == noErr );
  98. }
  99.  
  100. /*******************************************************************/
  101.  
  102. void    CPCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  103.                         pathType what, int16 vRefNum, int32 dirID )
  104. {
  105. char    sName[ 64 ], dName[ 64 ];
  106. OSErr     err;
  107. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  108.  
  109.     if( UserAbort() )
  110.         cpAbort = TRUE;
  111.  
  112.     if( cpAbort )
  113.         return;
  114.  
  115.     strcpy( sName, last );
  116.     strcpy( dName, last );        /* keep the same name in the new directory */
  117.  
  118.     if( what == pathIsFile )
  119.         err = CopyFile( sName, dName, vRefNum, cpVRefNum, dirID, cpDirID );
  120.     else if( what == pathIsDir )
  121.         err = CopyDir( sName, dName, vRefNum, cpVRefNum, dirID, cpDirID );
  122.  
  123.     if( err )
  124.         procPrintf( ShellWh, ProcID, "cp : can't copy %s (%d).\n", last, err );
  125. }
  126.  
  127. /*******************************************************************/
  128.  
  129. void        CPFiles( WHandle ShellWh, int16 ProcID, char *argument )
  130. {
  131. ShellWindRec    **MyShell;
  132.  
  133.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  134.  
  135.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) CPCallBack,
  136.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  137.  
  138.     ResetShellPWD( ShellWh );
  139. }
  140.  
  141. /*******************************************************************/
  142.  
  143. Boolean        CPFrom1To2( WHandle ShellWh, int16 ProcID, char *file1, char *file2 )
  144. {
  145. int16        srcVref, dstVref, err;
  146. int32        srcDirID, dstDirID, temp;
  147. pathType    pt;
  148. char        sName[ 64 ], dName[ 64 ], *file;
  149.  
  150.     pt = SetCurrPath( file1 );
  151.     if( pt == pathIsFile )
  152.         {
  153.         strcpy( sName, GetLastScan() );
  154.         GetPWDInfo( &srcVref, &srcDirID, &temp );
  155.         ResetShellPWD( ShellWh );
  156.  
  157.         pt = SetCurrPath( file2 );
  158.         if( pt != pathIsDir )
  159.             {
  160.             GetPWDInfo( &dstVref, &dstDirID, &temp );
  161.  
  162.             if( pt == pathIsFile )        /* file exists, delete it */
  163.                 {
  164.                 strcpy( dName, GetLastScan() );
  165.                 CtoPstr( dName );
  166.                 err = HDelete( dstVref, dstDirID, dName );
  167.                 if( err )
  168.                     procPrintf( ShellWh, ProcID, "cp : can't delete %ps (%d).\n", dName, err );
  169.                     
  170.                 PtoCstr( dName );
  171.                 }
  172.             else    /* does not exist, use given name */
  173.                 {
  174.                 file = ExtractFile( file2 );
  175.                 strcpy( dName, file );
  176.                 }
  177.                 
  178.             err = CopyFile( sName, dName, srcVref, dstVref, srcDirID, dstDirID );
  179.             if( err )
  180.                 procPrintf( ShellWh, ProcID, "cp : can't copy %s (%d).\n", file1, err );
  181.             }
  182.         else
  183.             procPrintf( ShellWh, ProcID, "cp : can't copy directory %s.\n", sName );
  184.         }
  185.  
  186.     ResetShellPWD( ShellWh );
  187. }
  188.  
  189. /*******************************************************************/
  190.  
  191. Boolean            DoCP( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  192. {
  193. int16            i, argc;
  194. char            *cp, argument[ 256 ];
  195. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  196.  
  197.     switch( ProcToken )
  198.         {
  199.         case    PROC_INIT    :
  200.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  201.             break;
  202.             
  203.         case    PROC_TERM    :
  204.         case    PROC_BREAK    :
  205.             cpAbort = TRUE;
  206.             /* Tell the shell that we're done */
  207.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  208.             /* Turn ourself off */
  209.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  210.             break;
  211.             
  212.         case    PROC_STDIN    :
  213.             if( (**MyShell).Proc[ ProcID ].flags )
  214.                 {
  215.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  216.                 argc = (**MyShell).Proc[ ProcID ].argc;
  217.                 cpAbort = FALSE;
  218.                 
  219.                 GetArgv( ShellWh, ProcID, argc-1, argument );
  220.                 if( CPSetDestDir( ShellWh, ProcID, argument ) )
  221.                     {
  222.                     /*
  223.                      *    Copy each file to the destination directory
  224.                      */
  225.                     for( i = 1; i < argc-1; i++ )
  226.                         {
  227.                         GetArgv( ShellWh, ProcID, i, argument );
  228.                         CPFiles( ShellWh, ProcID, argument );
  229.                         }
  230.                     }
  231.                 else if( argc == 3 )
  232.                     {    /* last argument was a file and two arguments */
  233.                     char     dstfile[ 256 ];
  234.                     GetArgv( ShellWh, ProcID, 1, argument );
  235.                     GetArgv( ShellWh, ProcID, 2, dstfile );
  236.                     CPFrom1To2( ShellWh, ProcID, argument, dstfile );
  237.                     }
  238.  
  239.                 /* Tell the shell that we're done */
  240.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  241.                 
  242.                 /* Turn ourself off */
  243.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  244.                 return( FALSE );
  245.                 }
  246.         }
  247. }
  248.  
  249.  
  250. /*******************************************************************
  251.  *
  252.  *    Function MV
  253.  *
  254.  *    PathName Callback function
  255.  *
  256.  *    usage mv file file
  257.  *          mv files directory
  258.  *
  259.  *******************************************************************/
  260.  
  261. #define        mvVRefNum    (**MyShell).Proc[ProcID].int0
  262. #define        mvDirID        (**MyShell).Proc[ProcID].long0
  263. #define        mvAbort        (**MyShell).Proc[ProcID].bflags.f0
  264.  
  265. Boolean        MVSetDestDir( WHandle ShellWh, int16 ProcID, char *argument )
  266. {
  267. int16        err;
  268. pathType    pt;
  269. char        dirPath[ 256 ];
  270. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  271.  
  272.     strcpy( dirPath, argument );
  273.     pt = SetCurrPath( dirPath );
  274.     if( pt == pathIsDir )
  275.         {
  276.         err = HGetVol( NULL, &mvVRefNum, &mvDirID );
  277.         if( err )
  278.             FileError( err );
  279.         }
  280.     else
  281.         err = 1;
  282.  
  283.     ResetShellPWD( ShellWh );
  284.     return( err == noErr );
  285. }
  286.  
  287. /*******************************************************************/
  288.  
  289. void        MVCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  290.                         pathType what, int16 vRefNum, int32 dirID )
  291. {
  292. char    sName[ 64 ], dName[ 64 ];
  293. OSErr     err;
  294. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  295.  
  296.     if( UserAbort() )
  297.         mvAbort = TRUE;
  298.  
  299.     if( mvAbort )
  300.         return;
  301.  
  302.     strcpy( sName, last );
  303.     strcpy( dName, last );        /* keep the same name in the new directory */
  304.  
  305.     err = MoveFile( sName, dName, vRefNum, mvVRefNum, dirID, mvDirID );
  306.     if( err )
  307.         procPrintf( ShellWh, ProcID, "mv : can't move %s (%d).\n", last, err );
  308. }
  309.  
  310. /*******************************************************************/
  311.  
  312. void        MVFiles( WHandle ShellWh, int16 ProcID, char *argument )
  313. {
  314. ShellWindRec    **MyShell;
  315.  
  316.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  317.  
  318.     ScanExpFwd( FALSE );    /* scan directories backwards to do wildcard moves */
  319.     
  320.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) MVCallBack,
  321.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  322.             
  323.     ScanExpFwd( TRUE );
  324.  
  325.     ResetShellPWD( ShellWh );
  326. }
  327.  
  328. /*******************************************************************/
  329.  
  330. Boolean    MVFrom1To2( WHandle ShellWh, int16 ProcID, char *file1, char *file2 )
  331. {
  332. int16        srcVref, dstVref, err;
  333. int32        srcDirID, dstDirID, temp;
  334. pathType    pt;
  335. char        sName[ 64 ], dName[ 64 ], *file;
  336.  
  337.     pt = SetCurrPath( file1 );
  338.     if( pt == pathIsFile )
  339.         {
  340.         strcpy( sName, GetLastScan() );
  341.         GetPWDInfo( &srcVref, &srcDirID, &temp );
  342.         ResetShellPWD( ShellWh );
  343.  
  344.         pt = SetCurrPath( file2 );
  345.         if( pt != pathIsDir )
  346.             {
  347.             GetPWDInfo( &dstVref, &dstDirID, &temp );
  348.  
  349.             if( pt == pathIsFile )        /* file exists, delete it */
  350.                 {
  351.                 strcpy( dName, GetLastScan() );
  352.                 CtoPstr( dName );
  353.                 err = HDelete( dstVref, dstDirID, dName );
  354.                 if( err )
  355.                     procPrintf( ShellWh, ProcID, "mv : can't delete %ps (%d).\n",
  356.                          dName, err );
  357.                     
  358.                 PtoCstr( dName );
  359.                 }
  360.             else    /* does not exist, use given name */
  361.                 {
  362.                 file = ExtractFile( file2 );
  363.                 strcpy( dName, file );
  364.                 }
  365.                 
  366.             err = MoveFile( sName, dName, srcVref, dstVref, srcDirID, dstDirID );
  367.             if( err )
  368.                 procPrintf( ShellWh, ProcID, "mv : can't move %s (%d)\n", file1, err );
  369.             }
  370.         else
  371.             procPrintf( ShellWh, ProcID, "mv : can't move directory %s.\n", sName );
  372.         }
  373.  
  374.     ResetShellPWD( ShellWh );
  375. }
  376.  
  377. /*******************************************************************/
  378.  
  379. Boolean            DoMV( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  380. {
  381. int16            i, argc;
  382. char            *cp, argument[ 256 ];
  383. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  384.  
  385.     switch( ProcToken )
  386.         {
  387.         case    PROC_INIT    :
  388.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  389.             break;
  390.             
  391.         case    PROC_TERM    :
  392.         case    PROC_BREAK    :
  393.             mvAbort = TRUE;
  394.             /* Tell the shell that we're done */
  395.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  396.             /* Turn ourself off */
  397.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  398.             break;
  399.             
  400.         case    PROC_STDIN    :
  401.             if( (**MyShell).Proc[ ProcID ].flags )
  402.                 {
  403.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  404.                 argc = (**MyShell).Proc[ ProcID ].argc;
  405.                 mvAbort = FALSE;
  406.                 
  407.                 GetArgv( ShellWh, ProcID, argc-1, argument );
  408.                 if( MVSetDestDir( ShellWh, ProcID, argument ) )
  409.                     {
  410.                     for( i = 1; i < argc-1; i++ )
  411.                         {
  412.                         GetArgv( ShellWh, ProcID, i, argument );
  413.                         MVFiles( ShellWh, ProcID, argument );
  414.                         }
  415.                     }
  416.                 else if( argc == 3 )
  417.                     {    /* last argument was a file and two arguments */
  418.                     char     dstfile[ 256 ];
  419.                     GetArgv( ShellWh, ProcID, 1, argument );
  420.                     GetArgv( ShellWh, ProcID, 2, dstfile );
  421.                     MVFrom1To2( ShellWh, ProcID, argument, dstfile );
  422.                     }
  423.  
  424.                 /* Tell the shell that we're done */
  425.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  426.                 
  427.                 /* Turn ourself off */
  428.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  429.                 return( FALSE );
  430.                 }
  431.         }
  432. }
  433.  
  434. /*******************************************************************
  435.  *
  436.  *    Function RM
  437.  *
  438.  *    PathName Callback function
  439.  *
  440.  *    usage RM [options] [names]    
  441.  *
  442.  *
  443.  *******************************************************************/
  444.  
  445. #define        rmForce        (**MyShell).Proc[ProcID].bflags.f0
  446. #define        rmAsk        (**MyShell).Proc[ProcID].bflags.f1
  447. #define        rmRecurse    (**MyShell).Proc[ProcID].bflags.f2
  448. #define        rmAbort        (**MyShell).Proc[ProcID].bflags.f3
  449.  
  450. void    RMCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  451.                         pathType what, int16 vRefNum, int32 dirID )
  452. {
  453. OSErr    err;
  454. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  455.  
  456.     if( UserAbort() )
  457.         rmAbort = TRUE;
  458.     
  459.     if( rmAbort )
  460.         return;
  461.  
  462.     if( what == pathIsFile )
  463.         {
  464.         CtoPstr( last );
  465.         
  466.         err = HDelete( vRefNum, dirID, last );
  467.         if( err )
  468.             FileError ( err );
  469.         }
  470.     else if( what == pathIsDir && rmRecurse )
  471.         {
  472.         err = RmTree( last, vRefNum, dirID );
  473.         if( err )
  474.             FileError ( err );
  475.         }
  476. }
  477.  
  478. /*******************************************************************/
  479.  
  480. void        RMFile( WHandle ShellWh, int16 ProcID, char *argument )
  481. {
  482. ShellWindRec    **MyShell;
  483. int16            matches = 1, lastMatch = 1;
  484.  
  485.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  486.  
  487.     ScanExpFwd( FALSE );    /* scan directories backwards to do wildcard moves */
  488.  
  489.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) RMCallBack,
  490.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  491.  
  492.     ScanExpFwd( TRUE );
  493.  
  494.     ResetShellPWD( ShellWh );
  495. }
  496.  
  497. /*******************************************************************/
  498.  
  499. Boolean            DoRM( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  500. {
  501. int16            i, argc;
  502. char            *cp, argument[ 256 ];
  503. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  504.  
  505.     switch( ProcToken )
  506.         {
  507.         case    PROC_INIT    :
  508.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  509.             break;
  510.             
  511.         case    PROC_TERM    :
  512.         case    PROC_BREAK    :
  513.             rmAbort = TRUE;
  514.             /* Tell the shell that we're done */
  515.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  516.             /* Turn ourself off */
  517.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  518.             break;
  519.             
  520.         case    PROC_STDIN    :
  521.             if( (**MyShell).Proc[ ProcID ].flags )
  522.                 {
  523.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  524.  
  525.                 rmForce    = FALSE;
  526.                 rmAsk    = FALSE;
  527.                 rmRecurse = FALSE;
  528.                 rmAbort    = FALSE;
  529.                 
  530.                 /* get arguments */
  531.                 argc = (**MyShell).Proc[ ProcID ].argc;
  532.                 for( i = 1; i < argc; i++ )
  533.                     {
  534.                     GetArgv( ShellWh, ProcID, i, argument );
  535.                     cp = argument;
  536.         
  537.                     if( *cp++ == '-' )
  538.                         while( *cp )
  539.                             switch( *cp++ )
  540.                                 {
  541.                                 case    'f'    :    /* force */
  542.                                     rmForce = TRUE;
  543.                                     break;
  544.                                     
  545.                                 case    'i'    :    /* ask for confirmation */
  546.                                     rmAsk = TRUE;
  547.                                     break;
  548.                                     
  549.                                 case    'r'    :    /* recursively delete directories */
  550.                                     rmRecurse = TRUE;
  551.                                     break;
  552.                                 }
  553.                     }
  554.  
  555.                 for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
  556.                     {
  557.                     GetArgv( ShellWh, ProcID, i, argument );
  558.                     if( *argument != '-' )
  559.                         RMFile( ShellWh, ProcID, argument );
  560.                     }
  561.  
  562.                 /* Tell the shell that we're done */
  563.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  564.                 
  565.                 /* Turn ourself off */
  566.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  567.                 return( FALSE );
  568.                 }
  569.         }
  570. }
  571.